package com.tom.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.tom.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallbackMethod")   //方法熔断
    @GetMapping("{id}")
    public User getUser(@PathVariable Long id) {
        String url = "http://user-server/user/" + id;
        return restTemplate.getForObject(url, User.class);
    }

    public User fallbackMethod(@PathVariable Long id) {

        return new User("系统繁忙", "123");
    }
}